package com.esp.webalbum.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.esp.webalbum.bean.ImageBean;
import com.esp.webalbum.image.ImageLoader;
import com.esp.webalbum.utils.Const;
import com.esp.webalbum.utils.Log;

public class WebalbumActivity extends Activity implements OnClickListener{
  
	private static String TAG = WebalbumActivity.class.getCanonicalName();
	
	private ArrayList<ImageBean> ftpArrayList = SplashScreen.arrayListImageBean;
	
	private ImageView imagview_webImage;
	
	private Button btn_previous,btn_next;
	
	private int Index = -1;
   
	private ImageLoader mImageLoader;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webactivity);
        Const.CONTEXT = WebalbumActivity.this;
        imagview_webImage = (ImageView)findViewById(R.id.imagview_webImage);
        btn_previous = (Button)findViewById(R.id.btn_previous);
        btn_next = (Button)findViewById(R.id.btn_next);
        
        
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	
    	if(ftpArrayList != null)
    	{	
    		mImageLoader = new ImageLoader(WebalbumActivity.this);
    		btn_next.setOnClickListener(this);	
    		btn_previous.setOnClickListener(this);
    		AsyncTask<Void, Void, Integer> downloadImage = new AsyncTask<Void, Void, Integer>() {
    			
    			
				@Override
				protected Integer doInBackground(Void... params) {
					
					int Result = 0;
					
		    		//increase  one number
		    		Index++;
		    		String URL = Const.SOUND_URL;
		    		URL += ftpArrayList.get(Index).getStrFileName();	
		    		Log.debug(TAG,"[ URL LINK : " + URL +"]");
		    		mImageLoader.DisplayImage(URL, imagview_webImage);
		    		
					return Result;
				}
			};
			downloadImage.execute();
    		
    	}
    }
    @Override
    protected void onPause() {
    	super.onPause();
    }
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    }

	public void onClick(View v) {
		
		switch (v.getId()) {
		
		case R.id.btn_next:
			if(Index < ftpArrayList.size())
			{
				Index ++;
				ImageViewer(Index);
				
			}
			break;
			
			
		case R.id.btn_previous:
			
			if(Index > 0){
				
				Index --;
				ImageViewer(Index);
			}
			break;

		default:
			break;
		}
	}
	
	public void ImageViewer(final int intPosition){
		
		if(ftpArrayList != null)
    	{	
    		mImageLoader = new ImageLoader(WebalbumActivity.this);
    		btn_next.setOnClickListener(this);	
    		btn_previous.setOnClickListener(this);
    		AsyncTask<Void, Void, Integer> downloadImage = new AsyncTask<Void, Void, Integer>() {
    			
    			
				@Override
				protected Integer doInBackground(Void... params) {
					
					int Result = 0;
					
		    		String URL = Const.SOUND_URL;
		    		URL += ftpArrayList.get(intPosition).getStrFileName();	
		    		Log.debug(TAG,"[ URL LINK : " + URL +"]");
		    		mImageLoader.DisplayImage(URL, imagview_webImage);
		    		
					return Result;
				}
			};
			downloadImage.execute();
    		
    	}
		
	}
}